package com.puccampinas.finalproject.core.initiate;

import java.io.IOException;

/**
 * Main entry point that will run the startup using the configuration passed 
 * by args or if there is no args, use homologation by default.
 * 
 * @author Tomas Zulberti (tomas.zulberti@movile.com)
 *
 */
public class Main {
    private static Startup startUp = null;
    
    protected static String DEFAULT_ENVIRONMENT = "testing";
    /**
     * Will create the startup with the configuration folder and start
     * all the required services.
     * 
     * <p>It will use the {@link Startup} with the production configuration using
     * the configuration properties.
     * 
     */
    public static void main(String[] args) {
    	String path = "conf/" + (args.length >= 1 ? args[0] : DEFAULT_ENVIRONMENT) + "/";
    	
        startUp = new Startup(path);
        
        try {
            startUp.startAll();
            
            if ("test".equals(System.getenv("ENVIRONMENT"))) {
                try {
                    System.in.read();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println("System going down!");
                System.exit(0);            
            } else {
                synchronized (Startup.class) {
                    try {
                        Startup.class.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
	                System.exit(0);
                    }
                }   
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
}